/*
 * Copyright (c) 2020 Fraunhofer FOKUS and others. All rights reserved.
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contact: mosaic@fokus.fraunhofer.de
 */
package org.eclipse.mosaic.interactions.mqtt;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.eclipse.mosaic.lib.util.JsonUtils;
import org.eclipse.mosaic.rti.api.Interaction;

import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE;

public class MqttPublish extends Interaction {

    private static final long serialVersionUID = 1L;

    /**
     * String identifying the type of this interaction.
     */
    public final static String TYPE_ID = createTypeIdentifier(MqttPublish.class);

    /**
     * topic mqtt message should be published to
     */
    private final String topic;

    /**
     * message. Will be json serialized
     */
    private final Object payload;

    /**
     * message qos: [0,1,2]
     */
    private final int qos;

    /**
     * retain message or not
     */
    private final boolean retained;

    public MqttPublish(long time, String topic, Object payload, int qos, boolean retained) {
        super(time);
        this.topic = topic;
        this.payload = payload;
        this.qos = qos;
        this.retained = retained;
    }

    public String getTopic() {
        return topic;
    }

    public Object getPayload() {
        return payload;
    }

    public int getQos() {
        return qos;
    }

    public boolean isRetained() {
        return retained;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(5, 83)
                .append(topic)
                .append(payload)
                .append(qos)
                .append(retained)
                .toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }

        MqttPublish mp = (MqttPublish) obj;
        return new EqualsBuilder()
                .append(this.topic, mp.topic)
                .append(this.payload, mp.payload)
                .append(this.qos, mp.qos)
                .append(this.retained, mp.retained)
                .isEquals();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, SHORT_PREFIX_STYLE)
                .appendSuper(super.toString())
                .append("topic", topic)
                .append("payload", JsonUtils.serialize(payload))
                .append("qos", qos)
                .append("retained", retained)
                .toString();
    }
}
